package com.cfjjc.gz.usraccount.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import com.cfjjc.gz.common.action.PagedAction;
import com.cfjjc.gz.common.consts.SystemConst;
import com.cfjjc.gz.common.exception.OTSException;
import com.cfjjc.gz.common.util.CreateEmailInfoUtil;
import com.cfjjc.gz.common.util.TreeConfig;
import com.cfjjc.gz.usraccount.service.A_ORGService;
import com.cfjjc.gz.usraccount.service.A_ORG_BONUSService;
import com.cfjjc.gz.usraccount.vo.A_ORGVO;
import com.cfjjc.gz.usraccount.vo.A_ORG_BONUSVO;
import com.cfjjc.gz.usraccount.vo.A_USRVO;

import java.util.logging.Level;

/**
 *
 * 说明：对 机构信息表 进行增删改查的前端处理类
 *
 * @author liuxun
 * @since 2012-03-06
 * @version 1.0
 */
public class A_ORG_BONUSAction extends PagedAction {

    private static final Logger logger = Logger.getLogger(A_ORG_BONUSAction.class);

    private A_ORGVO model1 = new A_ORGVO();
    
    protected int backupMessage;
    

	public int getBackupMessage() {
		return backupMessage;
	}

	public void setBackupMessage(int backupMessage) {
		this.backupMessage = backupMessage;
	}

	public void setModel(A_ORG_BONUSVO model) {
		this.model = model;
	}

	private A_ORG_BONUSVO model = new A_ORG_BONUSVO();

    public A_ORG_BONUSVO getModel() {
		return model;
	}

/*	public void setModel(A_ORG_BONUSVO model) {
		this.model = model;
	}*/
	private TreeConfig tree = new TreeConfig();
    private List r_List = new ArrayList();
 
    @Autowired
    private A_ORGService a_ORGService;
    
    @Autowired
    private A_ORG_BONUSService a_ORGBONUSService;

    public A_ORG_BONUSAction() {
        super();
        this.setQUERY_NAME("browseA_ORG_BONUSList");
    }

    private List<A_ORGVO> getAllORG() {
        try {
            rows = a_ORGService.queryA_ORGList(model1);
            return rows;
        } catch (OTSException ex) {
            java.util.logging.Logger.getLogger(A_ORG_BONUSAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

	// 显示用户信息表列表页面
 	public String queryList() throws Exception {
 		System.out.println("bonus list");
		preparePagedList(QUERY_NAME, getModel());
		if (!pagedRecords.exists()) {
			try {
				a_ORGBONUSService.queryA_USRs((A_ORG_BONUSVO) pagedRecords.getParam(),
						pagedRecords);
			} catch (OTSException e) {
				this.addActionError(e.getMessage());
				return ERROR;
			}
		}
		return LIST;
	}
 	
 	
 // 批量发放奖金
 	public String doPrizes() throws Exception {
 		try {
 			System.out.println("begin doPrizes");
 //		A_USRVO usr = (A_USRVO) session.get(SystemConst.SESSION_USER);
 			for (int n = 0; n < id.length; n++) {
 				A_ORG_BONUSVO param = new A_ORG_BONUSVO();
 				param.setKey(id[n]);
/* 				param.setUpdate_by(usr.getUsr_id());
 				a_USRService.doDeleteA_USR(param);*/
 				a_ORGBONUSService.doPrizesToUser(param);
 				
 				System.out.println("end doPrizes");
 			}
 		} catch (OTSException e) {
 			this.addActionError(e.getMessage());
 			return ERROR;
 		}
 		return NONE;
 	}
	 
	
    // 显示机构信息表管理首页
    public String showIndex() throws Exception {
        return INDEX;
    }
    
    private List dealTreeORG(A_ORGVO a) {
        String r_root = a.getUpper_org_code();

                for (Iterator iterator = rows.iterator(); iterator.hasNext();) {
                        A_ORGVO node = (A_ORGVO) iterator.next();
                        // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
                      
                      recursionFn(rows, node);
      
                }
                return r_List;

    }

    // 判断是否有子节点
    private boolean hasChild(List list, A_ORGVO node) {
        return getChildList(list, node).size() > 0 ? true : false;
    }
// 得到子节点列表

    private List getChildList(List list, A_ORGVO node) {
        List nodeList = new ArrayList();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            A_ORGVO n = (A_ORGVO) it.next();
            if (n.getUpper_org_code() == node.getOrg_code()) {
                nodeList.add(n);
            }
        }
        return nodeList;
    }
    private void recursionFn(List list, A_ORGVO node) {
                List childList = getChildList(list, node);// 得到子节点列表
                if (hasChild(list, node)) {// 判断是否有子节点
                        r_List.add(node);
                        Iterator it = childList.iterator();
                        while (it.hasNext()) {
                                A_ORGVO n = (A_ORGVO) it.next();
                                recursionFn(list, n);
                        }
                } else {
                        r_List.add(node);
                }
        }
    
    private A_ORG_BONUSVO reportmodel1;
    
    public String backup() throws Exception
    {
    	System.out.println("*********get Bonus Report begin***********");
    	
    	reportmodel1 = new A_ORG_BONUSVO();
    	
		rows = a_ORGBONUSService.queryA_ORGList(reportmodel1);

		System.out.println(" BONUS rows size:"+rows.size());
		
		A_ORG_BONUSVO testVo1;
		
		List listdata1 = new ArrayList();
		
		List listdatachild2;
		
		List listdatachild3 = new ArrayList();
		
		listdatachild3.add("身份证号");
		listdatachild3.add("姓名");
		listdatachild3.add("手机号");
		listdatachild3.add("当前奖金");
		listdatachild3.add("上月奖金");
		listdatachild3.add("上上月奖金");
		listdatachild3.add("奖金总数");
		listdatachild3.add("所属银行");
		listdatachild3.add("银行卡号");
 
		
		listdata1.add(listdatachild3);
		
		for(int j=0;j<rows.size();j++)
		{   
			testVo1 = new A_ORG_BONUSVO();
			
			listdatachild2 = new ArrayList();
			
			testVo1 = (A_ORG_BONUSVO) rows.get(j);

			listdatachild2.add(testVo1.getOrg_code());
			listdatachild2.add(testVo1.getOrg_name());
			listdatachild2.add(testVo1.getMobile());
        	listdatachild2.add(testVo1.getRemaining_bonus());
        	
			listdatachild2.add(testVo1.getLast_extraction_bonus());
			listdatachild2.add(testVo1.getLastlast_extraction_bonus());
			listdatachild2.add(testVo1.getTotle_bonus());
 
			listdatachild2.add(testVo1.getBankname());
			listdatachild2.add(testVo1.getBanknumber());
	
			

 
			 
			listdata1.add(listdatachild2);
		}
		
		
		
		
		
		
		
		/*CreateEmailInfoUtil.createEmailInfo("C:\\2013VW\\test2013.xls", listdata);*/

		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
		
		System.out.println("奖金xls生成时获得当前系统日期："+df.format(new Date()));// new Date()为获取当前系统时间
		
	//	CreateEmailInfoUtil.createEmailInfo("D:\\apache-tomcat-6.0.26\\apache-tomcat-6.0.26_2\\webapps\\cfjjc\\jsp\\usraccount\\a_html_view\\databackup"+df.format(new Date())+".xls", listdata,listdata1);
		
		CreateEmailInfoUtil.createBonus("D:\\apache-tomcat-6.0.26\\apache-tomcat-6.0.26_2\\webapps\\cfjjc\\jsp\\usraccount\\a_org_bonus\\bonus"+df.format(new Date())+".xls", listdata1);
		
		
		System.out.println("*********get Bonus Report end***********");
		
		this.setBackupMessage(1);
		
		return "backup";
        
    }

}
